package com.cjjd.c.feature.home.chekin.select_people;

import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.base.MyBaseAdapter;
import com.cjjd.c.bean.TravellerListBean;
import com.cjjd.c.utils.StringUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/16.
 */

public class SelectPeopleAdapter extends MyBaseAdapter {


    public SelectPeopleAdapter(FragmentActivity mContext) {
        super(mContext);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_select_people, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final TravellerListBean itemBean = (TravellerListBean) getItem(position);
        viewHolder.tvName.setText(itemBean.name);
        viewHolder.tvIdNum.setText("身份证 " + StringUtil.getSignIdNum(itemBean.idCardNum));
        viewHolder.checkbox.setChecked(itemBean.isSelected);


//        if (selectType == SelectPeopleActivity.SELECT_MULTI) {
//            // 可多选
//            viewHolder.checkbox.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    CheckBox checkbox = (CheckBox) v;
//                    int selectNum = 0;
//                    for (TravellerListBean item : (List<TravellerListBean>) data) {
//                        if (item.isSelected) selectNum++;
//                    }
//                    LogUtil.e("xyd","selectPeopleAdapter 已经选择了" + selectNum + "个");
//                    if (!checkbox.isChecked() && selectNum > unSelectNum) {
//                        ToastUtil.showToast("不能选择更多了");
//                    } else if (checkbox.isChecked()) {
//                        itemBean.isSelected = false;
//                    } else if (!checkbox.isChecked()) {
//                        itemBean.isSelected = true;
//                    }
//                    notifyDataSetChanged();
//                }
//            });
//
//
//        } else if (selectType == SelectPeopleActivity.SELECT_SIGNAL) {
//            // 只可单选
//            viewHolder.checkbox.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    for (TravellerListBean item : (List<TravellerListBean>) data) {
//                        item.isSelected = false;
//                        Log.e("xyd", "设置为未选中");
//                    }
//                    itemBean.isSelected = true;
//                    notifyDataSetChanged();
//                }
//            });
//
//
//        }


        return convertView;
    }

    private int selectType = -1;
    private int unSelectNum = -1;


    public void setSelectType(int selectType) {
        this.selectType = selectType;

    }

    public void setUnselectNum(int unSelectRoomNum) {
        this.unSelectNum = unSelectRoomNum;
    }


    static class ViewHolder {
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_id_num)
        TextView tvIdNum;
        @Bind(R.id.checkbox)
        CheckBox checkbox;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
